using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QTool.Inspector;
namespace QTool.NetFrame {
    public class FixCharacterController : QFixBehaviour
    {
        public QTool.BEPUphysics.Character.CharacterController controller;
        [QName("高度")]
        public Fix64 height = 1.7f;
        [QName("半径")]
        public Fix64 radius = 0.6f;
        [QName("边缘倒角")]
        public Fix64 margin = 0.1f;
        [QName("质量")]
        public Fix64 mass = 10;
        [QName("行走最大坡度")]
        public Fix64 maximumTractionSlope = 0.8f;
        [QName("静止最大坡度")]
        public Fix64 maximumSupportSlope = 1.3f;
        [QName("移速")]
        public Fix64 standingSpeed = 3;
        [QName("蹲下移速")]
        public Fix64 crouchingSpeed = 2;
        [QName("爬行移速")]
        public Fix64 proneSpeed = 1f;
        [QName("最大牵引力")]
        public Fix64 tractionForce = 1000;
        public Fix64 slidingSpeed = 6f;
        public Fix64 slidingForce = 50;
        public Fix64 airSpeed = 1;
        public Fix64 airForce = 250;
        [QName("跳跃起始速度")]
        public Fix64 jumpSpeed = 4.5f;
        public Fix64 slidingJumpSpeed = 3;
        public Fix64 maximumGlueForce = 5000;

        public FixVector2 HorizontalMoveDir
        {
            get => controller.HorizontalMotionConstraint.MovementDirection;
            set => controller.HorizontalMotionConstraint.MovementDirection = value;
        }
        //public override void OnFixUpdate()
        //{

        //    Debug.LogError(controller.ViewDirection);
        //    var v = Input["移动"].ReadVector2();
        //    controller.HorizontalMotionConstraint.MovementDirection = new FixVector2(v.x, v.y);
        //   // controller.Jump();
        //   // controller.Body.LinearVelocity = new FixVector3(v.x, controller.Body.LinearVelocity.y, v.y); Debug.LogError(controller.Body.LinearVelocity);
        //}
        public override void OnFixAwake()
        {
            base.OnFixAwake();
            controller = new QTool.BEPUphysics.Character.CharacterController(transform.position, height, height * 0.7f, height * 0.3f, radius,
            margin, mass, maximumTractionSlope, maximumSupportSlope, standingSpeed, crouchingSpeed, proneSpeed, tractionForce, slidingSpeed,
            slidingForce, airSpeed, airForce, jumpSpeed, slidingJumpSpeed, maximumGlueForce);
            NetEntity.fixEntity = controller.Body;
            NetEntity.spaceObject = controller;
            NetEntity.ControllerEntity = true;
            FixPhysics.space.Add(controller);
            
        }
        public override void OnFixDestroy()
        {
            base.OnFixDestroy();
            FixPhysics.space.Remove(controller);
        }
        public void OnDrawGizmosSelected()
        {
            Gizmos.color = Color.Lerp(Color.blue, Color.clear, 0.5f);
            Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
            //Gizmos.DrawLine()
            //Gizmos.DrawWireSphere(Vector3.up * (float)height / 2, (float)shape.Radius);
            Gizmos.DrawLine(Vector3.right * radius.Float + Vector3.down * height.Float / 2, Vector3.right * radius.Float + Vector3.up * height.Float / 2);
            Gizmos.DrawLine(-Vector3.right * radius.Float + Vector3.down * height.Float / 2, -Vector3.right * radius.Float + Vector3.up * height.Float / 2);
            Gizmos.DrawLine(Vector3.forward * radius.Float + Vector3.down * height.Float / 2, Vector3.forward * radius.Float + Vector3.up * height.Float / 2);
            Gizmos.DrawLine(-Vector3.forward * radius.Float + Vector3.down * height.Float / 2, -Vector3.forward * radius.Float + Vector3.up * height.Float / 2);
          //  Gizmos.DrawWireSphere(Vector3.down * (float)shape.Length / 2, (float)shape.Radius);

        }

    }
}
